package org.brownflat.lighthouse.ci.teamcity;

import org.brownflat.lighthouse.ci.Builder;
import org.brownflat.lighthouse.ci.ConnectionException;
import org.brownflat.lighthouse.ci.Feed;
import org.brownflat.lighthouse.ci.HTTPServer;
import org.brownflat.lighthouse.ci.Monitorable;

public class TeamCityServer extends HTTPServer implements Monitorable {

	private Builder<Feed, String> builder;

    public TeamCityServer(String host, int port, Builder<Feed, String> builder) {
        super(host, port);
        this.builder = builder;
    }

	public TeamCityServer(String host, int port, String path, Builder<Feed, String> builder) {
		super(host, port, path);
		this.builder = builder;
	}

	@Override
	public boolean hasAnyBrokenBuilds() {
		try {
			String result = execute(new TeamCityLatestBuildsRequest());
			Feed feed = builder.build(result);
			return feed.hasBrokenBuild();
		} catch (ConnectionException e) {
			throw new RuntimeException(e);
		}
	}

    @Override
    public String getName() {
        return "TeamCity";
    }
}
